<?php
/*
 * code to display a grid of a user's timesheets for a selected year
*/
//check user has logged in
include("../inc_files/utils/checksession.php");
// Create connection
include("../inc_files/utils/dbconnection.php");
//query for all customers
//if the year has been specified get it otherwise set it to this year
if(isset($_GET["year"])){
	$year = $mysqli->real_escape_string($_GET["year"]);
} else {
	$year = date("Y");
}
$userID = $_SESSION["userid"];

//if the page has been requested as a dashboard element page (for home page etc.)
//show only 10 records
if(isset($_GET["dashboard"])){
	$dashboard = $mysqli->real_escape_string($_GET["dashboard"]);
} else {
	$dashboard = 0;
}

if($dashboard == 0){
	$query = ("SELECT timesheet_periods.Closed, timesheet_periods.PeriodID, timesheet_periods.StartDate, T.TimesheetID
			FROM timesheet_periods LEFT JOIN (SELECT TimesheetID, TimesheetPeriod FROM timesheets
			WHERE timesheets.Timesheet_User = '{$userID}') AS T
			ON (timesheet_periods.PeriodID=T.TimesheetPeriod)
			WHERE LEFT(timesheet_periods.PeriodID,4) = '{$year}';");
} else {
	$minDate = date("Y-m-d", strtotime("-6 weeks"));
	$maxDate = date("Y-m-d", strtotime("+6 weeks"));

	$query = ("SELECT timesheet_periods.Closed, timesheet_periods.PeriodID, timesheet_periods.StartDate, T.TimesheetID
			FROM timesheet_periods LEFT JOIN (SELECT TimesheetID, TimesheetPeriod FROM timesheets
			WHERE timesheets.Timesheet_User = '{$userID}') AS T
			ON (timesheet_periods.PeriodID=T.TimesheetPeriod)
			WHERE timesheet_periods.StartDate > '{$minDate}' AND timesheet_periods.StartDate < '{$maxDate}';");
}
$result = $mysqli->query( $query );


//count the number of returned results
$resultCount = $result->num_rows;

//if there are some results returned create a table showing the results
if( $resultCount > 0){
	$rownumber = 0;

	echo "<table class='datagrid'>
			<tr class=\"datagrid headerrow\">

			<th style=\"width:30%\">Week number</th>
			<th style=\"width:30%\">Start Date</th>
			<th style=\"width:20%\">Hours</th>
			<th style=\"width:20%\">Create/Edit</th>
			</tr>";

	while($row = mysqli_fetch_array($result))
	{

		if($rownumber %2 == 0)
		{
			echo "<tr class=\"datagrid evenrow\">";
		}
		else
		{
			echo "<tr class=\"datagrid oddrow\">";
		}

		echo "<td style=\"width:30%;\" class=\"datagrid\">" . substr($row['PeriodID'], -2) . "</td>";
		echo "<td style=\"width:30%;\" class=\"datagrid\">" .  date_format(new DateTime($row['StartDate']), 'd/m/Y') .  "</td>";
		if(strlen($row['TimesheetID']) == 0 && $row['Closed'] != 1){
			echo "<td style=\"width:20%;\"></td>";
			echo "<td style=\"width:20%;\" class=\"datagrid\">";
			echo "<img src=\"../images/document-new-8.png\" alt=\"Create new timesheet for week beginning {$row['StartDate']}\" style=\"border: none; cursor:pointer;\" onclick=\"createTimesheet('{$row['PeriodID']}')\" ></td>";

		} else if (strlen($row['TimesheetID']) != 0 && $row['Closed'] != 1) {
			//as the timesheet has been created get the number of hours booke to it - edit timesheet
			$queryHours = ("SELECT SUM(SunHours+MonHours+TueHours+WedHours+ThurHours+FriHours+SatHours)
					AS TotalHours FROM timesheet_lines WHERE Timesheet = {$row['TimesheetID']}");
			$resultHours = mysqli_fetch_assoc(mysqli_query($mysqli,$queryHours));
			$hours = (strlen($resultHours['TotalHours']) > 0)? $resultHours['TotalHours']: 0;
			echo  "<td style=\"width:20%;\" class=\"datagrid\">". $hours  ."</td>";
			echo "<td style=\"width:20%;\" class=\"datagrid\">";
			
			echo "<a href='../timesheets/mytimesheet.php?id={$row['PeriodID']}' style=\"background-color:transparent;\"><img src=\"../images/pencil_24.png\" alt=\"Edit timesheet for week beginning {$row['StartDate']}\" style=\"border: none;\"></a></td>";
		} else if (strlen($row['TimesheetID']) != 0 && $row['Closed'] == 1) {
			//as the timesheet has been created get the number of hours booked to it - view only
			$queryHours = ("SELECT SUM(SunHours+MonHours+TueHours+WedHours+ThurHours+FriHours+SatHours)
					AS TotalHours FROM timesheet_lines WHERE Timesheet = {$row['TimesheetID']}");
			$resultHours = mysqli_fetch_assoc(mysqli_query($mysqli,$queryHours));
			$hours = (strlen($resultHours['TotalHours']) > 0)? $resultHours['TotalHours']: 0;
			echo  "<td style=\"width:20%;\" class=\"datagrid\">". $hours  ."</td>";
			echo "<td style=\"width:20%;\" class=\"datagrid\">";
			echo "<a href='../timesheets/mytimesheet.php?id={$row['PeriodID']}' style=\"background-color:transparent;\"><img src=\"../images/view_24.png\" alt=\"Edit timesheet for week beginning {$row['StartDate']}\" style=\"border: none;\"></a></td>";
		} else {
			echo"<td style=\"width:20%;\" colspan=\"2\"></td>";
		}

		echo "</tr>";
		$rownumber++;
	}
	echo "</table>";
}
//if no results returned say so.
else
{

	echo"No records returned";
}
//tidy up database connection
$mysqli->close();

//function to convert dates from dates entered by the user
//to dates for MySQL
function formatdate($date){
	$datearray = explode('/' , $date);
	$date = $datearray[2].'-'.$datearray[1].'-'.$datearray[0];
	return $date;

}

?>